package cn.iocoder.yudao.module.design.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.house.vo.*;
import cn.iocoder.yudao.module.design.convert.house.HouseConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.house.HouseDO;
import cn.iocoder.yudao.module.design.service.house.HouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "App  - 装修的房子信息")
@RestController
@RequestMapping("/design/house")
@Validated
public class AppHouseController {

    @Resource
    private HouseService houseService;

    @PostMapping("/create")
    @Operation(summary ="创建装修的房子信息")
    public CommonResult<Long> createHouse(@Valid @RequestBody HouseCreateReqVO createReqVO) {
        return success(houseService.createHouse(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新装修的房子信息")
    public CommonResult<Boolean> updateHouse(@Valid @RequestBody HouseUpdateReqVO updateReqVO) {
        houseService.updateHouse(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除装修的房子信息")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteHouse(@RequestParam("id") Long id) {
        houseService.deleteHouse(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得装修的房子信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<HouseRespVO> getHouse(@RequestParam("id") Long id) {
        HouseDO house = houseService.getHouse(id);
        return success(HouseConvert.INSTANCE.convert(house));
    }

    @GetMapping("/list")
    @Operation(summary ="获得装修的房子信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<HouseRespVO>> getHouseList(@RequestParam("ids") Collection<Long> ids) {
        List<HouseDO> list = houseService.getHouseList(ids);
        return success(HouseConvert.INSTANCE.convertList(list));
    }
    @GetMapping("/all")
    @Operation(summary ="获得装修的房子信息列表")
    @Parameter(name = "HouseBaseVO", description = "HouseBaseVO")
    public CommonResult<List<HouseRespVO>> getHouseAll(@Valid HouseBaseVO pageVO) {
        List<HouseRespVO> list = houseService.getHouseList(pageVO);
        return success(list);
    }

    @GetMapping("/page")
    @Operation(summary ="获得装修的房子信息分页")
    public CommonResult<PageResult<HouseRespVO>> getHousePage(@Valid HousePageReqVO pageVO) {
        PageResult<HouseDO> pageResult = houseService.getHousePage(pageVO);
        return success(HouseConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出装修的房子信息 Excel")
   
    public void exportHouseExcel(@Valid HouseExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<HouseDO> list = houseService.getHouseList(exportReqVO);
        // 导出 Excel
        List<HouseExcelVO> datas = HouseConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "装修的房子信息.xls", "数据", HouseExcelVO.class, datas);
    }



}
